package dao.impl;

import dao.AdminDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.Admin;
import pojo.User;
import utils.JdbcUtils;
import java.sql.SQLException;
import java.util.List;

/**
 * @author LiangHaiBo
 * @version 1.0
 * @date 2021.8.31 上午 8:42
 */
public class AdminDaoimpl implements AdminDao {
    @Override
    public Admin findAllByname(String name) {
        try {
            QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
            String sql ="select * from admin where adminname = ?";
            Admin admin = qr.query(sql, new BeanHandler<Admin>(Admin.class), name);
            return admin;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> selectUserAll() {
        try {
            QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
            String sql = "select * from user";
            List<User> list = qr.query(sql, new BeanListHandler<User>(User.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User getUserById(String uid) {
        try {
            QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
            String sql = "select * from user where uid = ?";
            User user = qr.query(sql, new BeanHandler<User>(User.class),uid);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void update(User user) {
        try {
            QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
            String sql ="update user set username=?,password=?,name=?,sex =? ,birthday=? ,state=? where uid  =?";
            qr.update(sql, user.getUsername(), user.getPassword(), user.getName(),
                    user.getSex(), user.getBirthday(), user.getState(), user.getUid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(String uid) {
        try {
            QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
            String sql = " delete from user where uid = ?";
            qr.update(sql, uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 针对用户分页查询
     * @param currentPage  当前页
     * @param pageSize     每页显示条数
     * @return             返回分页实体
     */
    @Override
    public List<User> findUserByPage(int currentPage, int pageSize) throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select * from user limit ?,?";
        List<User> list = qr.query(sql, new BeanListHandler<User>(User.class), (currentPage - 1) * pageSize, pageSize);
        return list;
    }

    /**
     * 查询总记录数
     * @return
     */
    @Override
    public int selectTotalCount() throws SQLException {
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select count(*) from user";
        String str = (qr.query(sql, new ScalarHandler<>())).toString();
        int count = Integer.parseInt(str);
        return count;
    }
}
